#pragma once
#include <assert.h>

namespace bit
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _prev;
		list_node<T>* _next;
	
		list_node(const T& val = T())
			:_data(val)
			,_prev(nullptr)
			,_next(nullptr)
		{}
	};

	template<class T,class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T,Ref,Ptr> Self;
		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{

		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		bool operator!= (const Self& s)
		{
			return _node != s._node;
		}

	};

	template <class T>
	class List
	{
		typedef list_node<T> Node;
		
	public:
		typedef list_iterator<T,T&,T*> iterator;
		typedef list_iterator<T,const T&,const T*> const_iterator;
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		} 
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}

		void empty_list()
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;
		}
		List()
		{
			empty_list();
		}
		List(size_t n, const T& val = T())
		{
			empty_list();
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		void push_back(const T& x)
		{
			Node* new_node = new Node(x);
			Node* tail = _head->_prev;

			new_node->_prev = tail;
			new_node->_next = _head;

			tail->_next = new_node;
			_head->_prev = new_node;
		}
	private:
		Node* _head;
	};
}
